package com.tzsafe.nfcdemo

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo

object NetCheckUtils {
    /**
     * 判断移动网络是否连接成功
     *
     * @param context
     * @return
     */
    fun isNetContected(context: Context): Boolean {
        val cm = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
        if (cm != null && info != null && info.isConnected) {
            return true
        }
        return false
    }

    /**
     * 判断WIFI是否连接成功
     *
     * @param context
     * @return
     */
    fun isWifiContected(context: Context): Boolean {
        val cm = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        if (info != null && info.isConnected) {
            return true
        }
        return false
    }

    fun isNetworkAvailable(): Boolean {
        val connectivityManager =
            appContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (connectivityManager == null) {
            return false
        } else {
            val networkInfo = connectivityManager.allNetworkInfo
            if (networkInfo != null && networkInfo.size > 0) {
                for (i in networkInfo.indices) {
                    if (networkInfo[i].state == NetworkInfo.State.CONNECTED) {
                        return true
                    }
                }
            }
        }
        return false
    }
}